<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="/resources/template/template.xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.org/ui"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:st="http://java.sun.com/jsf/composite/store">

    <ui:define name="content">
        <h:form>
            <h:panelGroup rendered="#{!webUserController.hasPrivilege('PharmacyOrderCreation')}" >
                You are NOT authorized
            </h:panelGroup>
            <p:panel rendered="#{webUserController.hasPrivilege('PharmacyOrderCreation')}" >
                <f:facet name="header">

                    <p:outputLabel value="Purchase order Request Order"/>
                    <p:commandButton ajax="false" value="Request" 
                                     action="#{storePurchaseOrderRequestController.request}"/>
                    <p:commandButton ajax="false"  value="New Order" 
                                     action="#{storePurchaseOrderRequestController.recreate}"/>
                    <hr></hr>
                    <p:spacer height="5" />
                    <h:panelGrid columns="2">
                        <h:outputLabel value="Select Payment Method"/>
                        <p:selectOneMenu   id="cmbPs" value="#{storePurchaseOrderRequestController.currentBill.paymentMethod}">    
                            <f:selectItem itemLabel="SelectPayment method"/>
                            <f:selectItems value="#{enumController.paymentMethodsForPo}"/>
                        </p:selectOneMenu>
                        <h:outputLabel value="Qutation Number"/>
                        <p:inputText value="#{storePurchaseOrderRequestController.currentBill.qutationNumber}"/>
                        <h:outputLabel value="Total "/>
                        <p:outputLabel id="tot" style="font-weight: bold;" value="#{storePurchaseOrderRequestController.currentBill.netTotal}">
                            <f:convertNumber pattern="#,##0.00"/>
                        </p:outputLabel>
                    </h:panelGrid>
                </f:facet>

                <p:tabView>
                    <p:tab title=" Distributor">                                               
                        <p:autoComplete converter="deal" 
                                        value="#{storePurchaseOrderRequestController.currentBill.toInstitution}"  
                                        forceSelection="true"
                                        completeMethod="#{storeDealorController.completeDealor}"
                                        var="vt" itemLabel="#{vt.name}" itemValue="#{vt}" >
                            <f:ajax event="itemSelect" execute="@this" render=":#{p:component('itemList')}" listener="#{storePurchaseOrderRequestController.setInsListener}"/>
                        </p:autoComplete>
                        <h:panelGroup>                            
                            <p:commandButton ajax="false" value="Fill with Item" action="#{storePurchaseOrderRequestController.createOrderWithItems}" disabled="#{storePurchaseOrderRequestController.currentBill.id ne null}"/>
                        </h:panelGroup>
                    </p:tab>

                    <p:tab title="Add Dealors Item ">
                        <p:autoComplete id="exDItem" value="#{storePurchaseOrderRequestController.currentBillItem.item}"
                                        forceSelection="true"
                                        completeMethod="#{itemController.completeDealorItem}" var="vt" itemLabel="#{vt.name}" itemValue="#{vt}" >
                            <p:column headerText="Item" >
                                <h:outputLabel value="#{vt.name}"></h:outputLabel>
                            </p:column>
                            <p:column headerText="Code" >
                                <h:outputLabel value="#{vt.code}"></h:outputLabel>
                            </p:column>
                            <p:column headerText="Pack Size" >
                                <p:outputLabel value=" X #{vt.dblValue}" 
                                               rendered="#{vt.class eq 'class com.divudi.entity.pharmacy.Ampp'}"/>                            
                            </p:column>
                        </p:autoComplete>
                        <p:commandButton value="Add Item" action="#{storePurchaseOrderRequestController.addItem}" process="exDItem @this" update="exDItem :#{p:component('itemList')} :#{p:component('tot')} " />

                    </p:tab>

                    <p:tab title="Add Extra Item ">
                        <p:autoComplete id="exItem" value="#{storePurchaseOrderRequestController.currentBillItem.item}"
                                        forceSelection="true"
                                        completeMethod="#{itemController.completeStoreItem}" var="vt" itemLabel="#{vt.name}" itemValue="#{vt}" >
                            <p:column headerText="Item" style="max-width: 600px;">
                                <h:outputLabel value="#{vt.name}"></h:outputLabel>
                            </p:column>
                            <p:column headerText="Code" style="max-width: 100px;overflow-wrap: break-word;">
                                <h:outputLabel value="#{vt.code}"></h:outputLabel>
                            </p:column>
                            <p:column headerText="Pack Size" style="max-width: 50px;">
                                <p:outputLabel value=" X #{vt.dblValue}" 
                                               rendered="#{vt.class eq 'class com.divudi.entity.pharmacy.Ampp'}"/>                            
                            </p:column>
                        </p:autoComplete>
                        <p:commandButton value="Add Item" action="#{storePurchaseOrderRequestController.addItem}" process="exItem @this" update="exItem :#{p:component('itemList')} :#{p:component('tot')}" />

                    </p:tab>
<!--                    <p:tab title="Payment Method + Qutation">
                        <h:panelGrid columns="2">
                            <h:outputLabel value="Select Payment Method"/>
                            <p:selectOneMenu   id="cmbPs" value="#{storePurchaseOrderRequestController.currentBill.paymentMethod}">    
                                <f:selectItem itemLabel="SelectPayment method"/>
                                <f:selectItems value="#{enumController.paymentMethodsForPo}"/>
                            </p:selectOneMenu>
                            <h:outputLabel value="Total "/>
                            <p:outputLabel id="tot" style="font-weight: bold;" value="#{storePurchaseOrderRequestController.currentBill.netTotal}">
                                <f:convertNumber pattern="#,##0.00"/>
                            </p:outputLabel>
                            <h:outputLabel value="Qutation Number"/>
                            <p:inputText value="#{storePurchaseOrderRequestController.currentBill.qutationNumber}"/>
                        </h:panelGrid>
                    </p:tab>-->

                </p:tabView>

                <p:dataTable styleClass="noBorder" rowIndexVar="i" 
                             var="bi" scrollable="true" rowKey="#{bi.searialNo}"
                             scrollHeight="250" value="#{storePurchaseOrderRequestController.billItems}" 
                             selection="#{storePurchaseOrderRequestController.selectedBillItems}"
                             id="itemList" >  

                    <f:facet name="header">  
                        <h:outputLabel  value="Store Bill Item"/>
                        <p:commandButton ajax="false" value="Remove All" action="#{storePurchaseOrderRequestController.removeSelected()}"/>
                    </f:facet>  

                    <p:column selectionMode="multiple" style="width: 10px;"></p:column>


                    <p:column headerText="No" style="width: 25px;">
                        #{bi.searialNo+1}
                    </p:column>

                    <p:column headerText="Item Name"  style="width:30%">  
                        #{bi.item.name} 
                    </p:column>  
                    #{a}
                    <p:column headerText="Qty" style="width:20%"> 
                        <p:inputText autocomplete="off"  id="qty" value="#{bi.tmpQty}" style="width:100%" label="Qty">                       
                            <f:ajax event="blur" render="@this total :#{p:component('tot')} "  execute="@this price" listener="#{storePurchaseOrderRequestController.onEdit(bi)}" ></f:ajax>
                            <f:ajax event="focus" render=":#{p:component('tab')}" listener="#{storePurchaseOrderRequestController.onFocus(bi)}" />
                        </p:inputText>
                    </p:column>  
                    <p:column headerText="Purchase Price" style="width:20%">  
                        <h:panelGroup id="price">
                            <p:inputText autocomplete="off" value="#{bi.pharmaceuticalBillItem.purchaseRate}">                                
                                <f:ajax event="keyup" render="total :#{p:component('tot')}"  execute="@this qty" listener="#{storePurchaseOrderRequestController.onEdit(bi)}" ></f:ajax>
                                <f:ajax event="focus" render=":#{p:component('tab')}" listener="#{storePurchaseOrderRequestController.onFocus(bi)}" />
                            </p:inputText>
                        </h:panelGroup>
                    </p:column>  

                    <p:column headerText="Total" style="width:20%">  
                        <h:panelGroup id="total">
                            <h:outputText value="#{bi.pharmaceuticalBillItem.purchaseRate*bi.pharmaceuticalBillItem.qty}" >                                
                            </h:outputText>
                        </h:panelGroup>
                    </p:column>  

                    <p:column style="width:10%">
                        <p:commandButton value="Remove" ajax="false" action="#{storePurchaseOrderRequestController.removeItem(bi)}"/>
                    </p:column>

                </p:dataTable>  
                <p:spacer height="50"/>
                <st:history/>

            </p:panel>            
        </h:form>
    </ui:define>  

</ui:composition>
